package cn.tedu.straw.portal.controller;


import cn.tedu.straw.portal.dto.StudentRegisterDTO;
import cn.tedu.straw.portal.ex.*;
import cn.tedu.straw.portal.service.IUserService;
import cn.tedu.straw.portal.util.R;
import cn.tedu.straw.portal.vo.TeacherVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2021-02-26
 */
@RestController
@RequestMapping("/portal/users")
public class UserController {
    @Autowired
    IUserService userService;
    @RequestMapping("/reg/student")
    public R studentRegister(@Valid StudentRegisterDTO studentRegisterDTO, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            throw new ParamValidationException(errorMessage);
        }
            userService.studentRegister(studentRegisterDTO);
        return R.ok();
    }
    @GetMapping("/teachers")
    public R getTeacherList(){
        List<TeacherVO> teachers = userService.getTeacherList();
        return R.ok(teachers);
    }

}
